#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <unistd.h>
#include <windows.h>
#include <graphics.h>
#include <ege/sys_edit.h>
using namespace std;
// 定义结构体
struct Falm {
	int year;
	string ju_ming;
	string ren_wu;
	string yan_yuan;
	string ping_tai;
	string shi_fou;
	string nv_zhu;
	string zhu_yan;
	string jin_du;
// 添加'<'运算符重载函数
	bool operator<(const Falm& other) const {
		return ju_ming < other.ju_ming;
	}
};
vector<Falm>  barm;
// 删除字符串中的特定字符(问号、0、斜杠后的内容)
void removeChars(string& str, const string& charsToRemove) {
	if (str.length() == 1) {
		size_t zero = str.find("0");
		if (zero != string::npos) {
			str.erase(zero, 1);
		}
	} else {
		size_t pos = str.find_first_of(charsToRemove);
		while (pos != string::npos) {
			str.erase(pos, 19);
			pos = str.find_first_of(charsToRemove, pos);
		}
	}
}

vector<Falm> dataList;
vector<int>id ;
// 读取CSV文件并存储数据到列表容器中
vector<Falm> readDataFromCSV(const string& fileName) {
	ifstream file(fileName);
	string line, cell;
	int i = 0;
	while (getline(file, line)) {
		int commaCount = 0;
		for (char c : line) {
			if (c == ',') {
				commaCount++;
				if (commaCount == 8) {
					line.clear(); // 删除这一行
					break;
				}
			} else {
				commaCount = 0; // 重置计数器
			}
		}
		if (!line.empty()) {
			istringstream iss(line);
			Falm data;
			for (int i = 1; i <= 9; i++) {
				getline(iss, cell, ',');
				removeChars(cell, "?/"); // 删除问号、0、斜杠后的内容
				switch (i) {
					case 1:
						try {
							data.year = stoi(cell);
						} catch (exception& e) {
							// 处理其他异常的代码
						}
						break;
					case 2:
						data.ju_ming = cell;
						break;
					case 3:
						data.ren_wu = cell;
						break;
					case 4:
						data.yan_yuan = cell;
						break;
					case 5:
						data.ping_tai = cell;
						break;
					case 6:
						data.shi_fou = cell;
						break;
					case 7:
						data.nv_zhu = cell;
						break;
					case 8:
						data.zhu_yan = cell;
						break;
					case 9:
						data.jin_du = cell;
						break;
					default:
//						cout <<  "error"  << endl;
						break;
				}
			}
//            cout <<  i  << endl;
			id.push_back(i);
			if (i == 0) {
				barm.push_back(data);
			} else
				dataList.push_back(data);
			i++;//行
		}
	}
	file.close();
	return dataList;
}

// 根据第一列年份排序数据
bool sortByYear(const Falm & first, const Falm & second) {
	return first.year < second.year;
}


#define w 368
#define h 678
int i;
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
const char* str[] = {"1 查看所有数据",
                     "2 添加影视信息",
                     "3 删除影视信息",
                     "4 修改一行数据",
                     "5 退出"
                    };

void init() {
	initgraph(w, h,  INIT_RENDERMANUAL | INIT_NOFORCEEXIT); //系统设置在图形模式
	movewindow((screenWidth - w ) / 2, (screenHeight - h) / 2, true);
	setbkcolor(WHITE);
	setcaption("仙侠剧中的高颜值角色");
	PIMAGE img = newimage();
	getimage_pngfile(img, "00580-1078175348-__beautiful_face____extremely_delicate_facial__best_quality___extremely_detailed_cg_8k_wallpaper__sitting__cowboy_shot__masterpi-0000-removebg-preview.png");
	putimage_withalpha(NULL, img,  0, 0,  0, 0, getwidth(img), getheight(img));
	setcolor(RED);
	setfillcolor(RED);
	delimage(img);
	return ;
}
void draw_rectangle() {
	if (i == 4) {
		rectangle(w / 2 - 35, (i + 4) * (h / 9) - 2, w / 2 + 35, (i + 4) * (h / 9)  + 30 + 2);
	} else {
		rectangle(w / 3 - 14, (i + 4) * (h / 9) - 2, w - w / 3 + 14, (i + 4) * (h / 9)  + 30 + 2); //，空心矩形的左上角坐标右下角坐标
	}
	outtextxy(w / 2, (i + 4) * (h / 9), str[i]); //，在坐标点左上角处打印文字
}

void menu_style(color_t color) {
	setfont(30, 0, "微软雅黑"); //高100，宽不指定，字体
	setfontbkcolor(color);
	settextjustify(CENTER_TEXT, TOP_TEXT);
}
void menu(color_t color) {
	menu_style(color);
	draw_rectangle();
}

void show(vector<Falm> dataList) {
	resizewindow(w * 3, h);
	movewindow((screenWidth - w * 3) / 2, (screenHeight - h) / 2, true);
	setbkcolor(LIGHTCYAN );
	int rows = 15;
	int columns = 9;
	int z = 200;
	int s = 80;
//	int y=z+90;
	int x = 37;
	setbkmode(TRANSPARENT);
	for (int i = 0; i <= rows; i++) {

		setcolor(RED);
		setfont(20, 0, "微软雅黑");
		double arr[9]; // 定义一个长度为 9 的数组
		arr[0] = 1.1; // 将 c=1.1 存入数组的第一个元素
		for (int i = 1; i < 9; i++) {
			arr[i] = arr[i - 1] + 2.2; // 将 c+=2.2 存入数组的其他元素
		}
		for (int i = 0; i < 9; i++) {
			arr[i] *= x;
			arr[i] += z;
		}
		// 输出数组中的元素
		int y = x * i + 46;
		map<int, string>bar;
		bar[0] = barm[0].year;
		bar[1] = barm[0].ju_ming;
		bar[2] = barm[0].ren_wu;
		bar[3] = barm[0].yan_yuan;
		bar[4] = barm[0].ping_tai;
		bar[5] = barm[0].shi_fou;
		bar[6] = barm[0].nv_zhu;
		bar[7] = barm[0].zhu_yan;
		bar[8] = barm[0].jin_du;
		if (i == 0) {
			setcolor(GREEN);
			for (int k = 0; k < 9; k++) {
				xyprintf((1.1 + 2.2 * k)*x + z, y, "%s", k == 0 ? "年代" : bar[k].c_str());
			}
		}
		for ( int j = 0; j <= columns; j++) {
			rectangle(z, x * i + 36, z + s * j, x * (i + 2)); //表格
//			rectangle(z,x*i+36,y,x*(i+2));//列表
//			rectangle(z,36,z+s*j,79);//行表
//			rectangle(z,x,z+s*j,x * 2);//行表
		}

		setcolor(BLUE);
		int zl = z - 40;
		int zll = x;
		rectangle(zl, zll * i + 36, zl + 30, zll * (i + 2));
		if (i == 0) {
			setcolor(GREEN);
			rectangle(zl, zll * i + 36, zl + 30, zll * (i + 2));
			xyprintf(zl + 14, zll * i + 46, "%s", "id");
		} else {
			xyprintf(zl + 14, zll * i + 46, "%d", id[i]);
		}

		if (i == rows) {
			continue;
		}
		int yy = x * (i + 1) + 46;
		setcolor(RED);
		xyprintf(arr[0], yy, "%d", dataList[i].year);
		xyprintf(arr[1], yy, "%s", dataList[i].ju_ming.c_str());
		xyprintf(arr[2], yy, "%s", dataList[i].ren_wu.c_str());
		xyprintf(arr[3], yy, "%s", dataList[i].yan_yuan.c_str());
		xyprintf(arr[4], yy, "%s", dataList[i].ping_tai.c_str());
		xyprintf(arr[5], yy, "%s", dataList[i].shi_fou.c_str());
		xyprintf(arr[6], yy, "%s", dataList[i].nv_zhu.c_str());
		xyprintf(arr[7], yy, "%s", dataList[i].zhu_yan.c_str());
		xyprintf(arr[8], yy, "%s", dataList[i].jin_du.c_str());
	}
	getch();
	setbkmode(OPAQUE);
}
string in(const string& s, const string& ss) {
	initgraph(400, 300);
	movewindow((screenWidth - 400 ) / 2, (screenHeight - 300) / 2, true);
	char str[100] = "";
	inputbox_getline(s.c_str(), ss.c_str(), str, sizeof(str) / sizeof(*str));
	// 将字符数组转换为字符串
	string result(str);
	return result;
}

vector<string> bar_menu = {"年代", "剧名", "人物", "演员", "平台", "是否", "女主", "主演", "进度"};
string fileName = "仙侠剧中的高颜值角色.csv";
const char *filename = fileName.c_str();
void sortData(vector<Falm> dataList) {
	remove(filename);
	ofstream a(filename);
	for (unsigned i = 0; i < bar_menu.size(); i++) {
		if (i == bar_menu.size() - 1) {
			a << bar_menu[i];
		} else
			a << bar_menu[i] << ",";
	}
	a << endl;
	set<Falm> s(dataList.begin(), dataList.end());
	dataList.assign(s.begin(), s.end());
// 按照年份排序
	sort(dataList.begin(), dataList.end(), sortByYear);
	for (const Falm& data : dataList) {
		a << data.year << ",";
		a << data.ju_ming << ",";
		a << data.ren_wu << ",";
		a << data.yan_yuan << ",";
		a << data.ping_tai << ",";
		a << data.shi_fou << ",";
		a << data.nv_zhu << ",";
		a << data.zhu_yan << ",";
		a << data.jin_du << endl;
	}
	a.close();
}
void event() {
	readDataFromCSV(fileName);
	
	string q[9];
	for (int i = 0; i <= 8; i++) {
		q[i] = "请输入" + bar_menu[i] + "，输入完请回车";
	}
	switch (i + 1) {
		case 1:
			break;
		case 2:
			//TODO添加数据
			try {
				Falm f;
				string t = "添加数据";
			f.year = stoi(in(t, q[0]));
				f.ju_ming = in(t, q[1]);
				f.ren_wu = in(t, q[2]);
				f.yan_yuan = in(t, q[3]);
				f.ping_tai = in(t, q[4]);
				f.shi_fou = in(t, q[5]);
				f.nv_zhu = in(t, q[6]);
				f.zhu_yan = in(t, q[7]);
				f.jin_du = in(t, q[8]);

				dataList.push_back(f);
			} catch (...) {
				break;}
			break;
		case 3:
			//TODO删除
			try {
				int ins = stoi(in("删除哪一行？", "请输入所在行，输入完请回车"));
				ins -= 1;
				dataList.erase(dataList.begin() + ins);
			} catch (...) {break;}
			break;
		case 4:
			//TODO修改数据
			try {
				int i = stoi(in("修改哪一行？", "请输入所在行，输入完请回车"));
				i -= 1;
				string x = "修改数据";
		dataList[i].year = stoi(in(x, q[0]));
				dataList[i].ju_ming = in(x, q[1]);
				dataList[i].ren_wu = in(x, q[2]);
				dataList[i].yan_yuan = in(x, q[3]);
				dataList[i].ping_tai = in(x, q[4]);
				dataList[i].shi_fou = in(x, q[5]);
				dataList[i].nv_zhu = in(x, q[6]);
				dataList[i].zhu_yan = in(x, q[7]);
				dataList[i].jin_du = in(x, q[8]);

				cout << endl;
			} catch (...) {break;}
			break;
		case 5:
			exit(0);
			break;
		default:
			break;
	}
	sortData(dataList);
	HWND hWnd = getHWnd();
// 使用 API 函数修改窗口名称
	SetWindowText(hWnd, TEXT("文件列表"));
	show(dataList);
	init();
}

void button(mouse_msg msg, int x, int y, int width, int height) {
	if (msg.x >=  x && msg.y >= y && msg.x <= width && msg.y <= height) {
		menu(WHITE);
		if (msg.is_left() && msg.is_up()) {
			event();
			menu(RGB(248, 171, 166));
		} else if (msg.is_left() && msg.is_down())menu(LIGHTCYAN);
	} else
		menu(PINK);
}

int main(void) {
	init();
	for (; is_run(); delay_fps(60)) {
		while (mousemsg()) {
			mouse_msg msg = getmouse();
			for (i = 0; i < 5; i++)
				if (i == 4) {
					//TODO获取鼠标消息
					button(msg, w / 2 - 35, (i + 4) * (h / 9) - 2, w / 2 + 35, (i + 4) * (h / 9)  + 30 + 2);
				} else
					button(msg, w / 3 - 14, (i + 4) * (h / 9) - 2, w - w / 3 + 14, (i + 4) * (h / 9)  + 30 + 2); //，空心矩形的左上角坐标右下角坐标

		}
	}
	closegraph();//关闭图形界面
	return 0;
}
